<?php
/**
 * Created by 上海风车教育科技有限公司.
 * User: 刘红伟
 * Date: 15-11-25
 * Email: 454303753@qq.com
 * File:wechatqrcode.php
 */
class wechatqrcode{
    private $url;
    private $token;
    private $json_data;
    public function __construct($token,$scene_str){
        $this->token=$token;
        $this->setUrl();
        $this->setVal($scene_str);
    }
    public function setUrl(){
        $url="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->token;
        $this->url=$url;

    }
    public function create(){

        $http 	=  wx_http::factory($this->url, wx_http::TYPE_CURL);
        $json 	= $http->post($this->url, $this->json_data, array(
            'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)',
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: '. strlen($this->json_data)
        ));
        $result	= json_decode($json,true);

        if ((isset($result['errcode']) ) OR (isset($result['errmsg'])))
        {
            $this->_errno = $result['errcode'];
            $this->_error = $result['errmsg'];

            return FALSE;
        }
       return $this->getTicket($result['ticket']);

    }
    public function setVal($scene_str){
        $data=array();
        $data['action_name']='QR_LIMIT_STR_SCENE';
        $data['action_info']=array('scene'=>array('scene_str'=>$scene_str));
        $this->json_data    =   json_encode($data);
    }

    protected  function getTicket($ticket){
         return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
    }


}